package com.xtz.controller;

import com.xtz.domain.DishFlavor;
import com.xtz.service.AdminDishService;
import com.xtz.service.SetmealService;
import com.xtz.vo.DishVo;
import com.xtz.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("dish")
public class DishController {
    @Autowired
    private AdminDishService adminDishService;

    @GetMapping("/list")
    public Result getList(@RequestParam String categoryId,
                          @RequestParam Integer status){
        //根据分类id，查出所有菜品
        List<DishVo> dishList = adminDishService.getListByTwo(categoryId,status);
        //查出所有菜品信息
        List<DishVo> voList = new ArrayList<>();
        //每条菜品都会对应一个口味
        for (DishVo dishVo : dishList) {
            //获取每次遍历到的菜品id
            String dishId = dishVo.getId();
            //根据每次遍历到的菜品id，去查询对应的口味信息
            List<DishFlavor> flavors = adminDishService.getFlavorByDishId(dishId);
            //将数据组装到vo结构中
            DishVo vo = new DishVo();
            vo.setFlavors(flavors);
            BeanUtils.copyProperties(dishVo,vo);

            voList.add(vo);


        }
        return new Result(1,null,voList,null);
    }



}
